//
//  YDPickerAlertView.m
//  YunDongGym
//
//  Created by lqq on 2018/6/7.
//  Copyright © 2018年 lqq. All rights reserved.
//

#import "YDPickerAlertView.h"

@interface YDPickerAlertView ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UIPickerView *pickView;
@property (weak, nonatomic) IBOutlet UIButton *cancleBtn;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;

@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, assign) NSInteger defaultIndex;
@property(nonatomic ,copy) void(^selectBlock)(NSInteger selectIndex);
@end

@implementation YDPickerAlertView

+ (void)showPickViewWithDataSource:(NSArray *)dataSource defaultIndex:(NSInteger)defaultIndex selectBlock:(void(^)(NSInteger selectIndex))selectBlock
{
    YDPickerAlertView *alertView = [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([YDPickerAlertView class]) owner:nil options:nil] lastObject];
    alertView.dataSource = dataSource;
    alertView.defaultIndex = defaultIndex;
    alertView.selectBlock = selectBlock;
    [alertView show];
}

-(void)awakeFromNib
{
    [super awakeFromNib];
    _pickView.delegate = self;
    _pickView.dataSource = self;
    _cancleBtn.layer.cornerRadius = 2;
    _cancleBtn.layer.masksToBounds = YES;
    [_cancleBtn addBorderWithBorderWidth:kOnePX borderColor:HEXColor(@"#9B9B9B")];
    _confirmBtn.layer.cornerRadius = 2;
    _confirmBtn.layer.masksToBounds = YES;
}

- (void)show{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.frame = window.bounds;
    [window addSubview:self];
    [_pickView selectRow:_defaultIndex inComponent:0 animated:NO];
    self.backgroundColor = [UIColor clearColor];
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    }];
}
- (IBAction)cancleBtnClicked:(id)sender {
    [self removeFromSuperview];
}

- (IBAction)confirmBtnClicked:(id)sender {
    NSInteger index = [self.pickView selectedRowInComponent:0];
    if (_selectBlock) {
        _selectBlock(index);
    }
    [self removeFromSuperview];
    
}

#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _dataSource.count;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    //设置分割线的颜色
    for(UIView *singleLine in pickerView.subviews)
    {
        if (singleLine.frame.size.height < 1)
        {
            singleLine.backgroundColor = [UIColor colorWithHexString:@"DFDFDF"];
        }
    }
    
    //设置文字的属性
    UILabel *label = [UILabel new];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
    label.text = _dataSource[row];
    
    
    return label;
}

@end
